Timestamped Plugin - Reference Documentation
Authors: Diego Toharia
Version: 0.4
1 Introduction
The Timestamped plugin aims to solve a small yet recurrent DRY problem when using Grails: the generation of the auto-timestamping properties, which right now has to be explicitly declared along with other properties in a domain class. This has two problems:- These properties aren't part of the domain class logic, so don't provide value to anyone examining the class.
- If you wan't to switch from the standard class (java.util.Date) to joda-time, you would have to edit all the domain classes to change the property type.
2 Installation
Install as a plugin
You can add the plugin to your project as a dependency by editing your project's BuildConfig.groovy as the following:plugins { // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g. … compile 'timestamped:0.4' }
Install as a maven dependency
You can add the plugin as a precompiled maven dependency by editing your project's BuildConfig.groovy as the following:repositories { … mavenRepo 'http://deigote.github.io/grails-timestamped/maven/releases/' grailsPlugins() } … dependencies { … compile 'com.deigote.grails-plugins:timestamped:0.4' }
3 Usage
Basic usage
Using the plugin is fairly simple. You just need to annotate the domain classes as Timestamped and the dateCreated and lastUpdated properties will be injected at compile time:package my.awesome.grails.appimport com.tado.timestamped.transform.Timestamped@Timestamped class Car { Brand brand String modelName }
Specifiying the properties to inject
By default, all the autotimestamping properties will be injected (at the moment, Grails provides support for dateCreated and lastUpdated ), but you can specify which one you want to avoid:- If you don't want the lastUpdated property to be injected, you can specify to ignore the update event:
package my.awesome.grails.appimport com.tado.timestamped.transform.Timestamped@Timestamped(update=false) // We won't update this, so we don't need the last updated date class Brand { String name Logo logo }
- Accordingly If you don't want the dateCreated property to be injected, you can specify to ignore the update event:
package my.awesome.grails.appimport com.tado.timestamped.transform.Timestamped@Timestamped(create=false) // We don't care about the creation date class Car { Brand brand String modelName }
Specifiying the class of the properties to inject
The Timestamped plugin will try to use the joda-time org.joda.time.Instant class for the timestamped properties, and will fallback to the old and not so good java.util.Date if the previous one is not found, but you can specify your own class:package my.awesome.grails.appimport com.tado.timestamped.transform.Timestamped@Timestamped(clazz='java.lang.Long') class Brand { String name Logo logo }
- Your own specified class
- org.joda.time.Instant class
- java.util.Date class